package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import csc.daonjpa.java.domain.Account;

/**
 * DAO class for Account entity. This class contains all methods that inserts/updates/deletes
 * account info
 * @author nvu3
 *
 */
@Repository("AccountDAO")
public class AccountDAO {
	
	@PersistenceContext
	EntityManager entityManagerFactory;
	
	/**
	 * @return all accounts from Account table
	 */
	@Transactional
	public List<Account> getAccounts() {
		
		List<Account> accounts = null;
		try {
			String sql = "SELECT a FROM Account a";
			TypedQuery<Account> query = entityManagerFactory.createQuery(sql, Account.class);
			accounts = query.getResultList();
		} catch (Exception e) {
			
		}
		
		return accounts;
	}
	
	/**
	 * Add account to Account table
	 * @param account Account entity that contains mapped with each column in Account table
	 * @return
	 */
	public boolean addAccount(Account account) {
		// Obtains entity manager object
//		EntityManager entityManager = EntityManagerFactoryUtil.createEntityManager();
//		
//		// Obtains transaction from entity manager
//		EntityTransaction entr = entityManager.getTransaction();
//		
//		try {
//			// -----------Begin transaction-----------
//			entr.begin();
//			// Insert a row to Account table
//			entityManager.persist(account);
//			entr.commit();
//		} catch (Exception e) {
//			entityManager.close();
//		}
//		// -----------End transaction-----------
//		
		return true;
	}
}
